$(function () {
  bindUserToForm()
  //用户名失去焦点时执行的验证操作
  //供应商编码
  $('#proCode').blur(checkProCode)
  //供应商名称
  $('#proName').blur(checkProName)
  //联系人
  $('#proContact').blur(checkProContact)
  //联系电话
  $('#proPhone').blur(checkProPhone)
  //联系地址
  $('#proAddress').blur(checkProAddress)
  //传真
  $('#proFax').blur(checkProFax)
  //描述
  $('#proDesc').blur(checkProDesc)
  //表单提交
  $('.updateForm').submit(function (e) {
    e.preventDefault()
    //判断当前的表单验证是否通过
    let flag = checkProCode() & checkProName() & checkProContact() & checkProPhone() & checkProAddress() & checkProFax() & checkProDesc()
    if (flag === 0) {
      return alert('请正确填写修改信息')
    }
    //提交表单进行修改操作
    updateUser()
  })
})


/**
 * 修改用户信息的函数
 * @date 2021-05-08
 * @returns {any}
 */
function updateUser() {
  let {
    id
  } = getParams()
  //准备参数
  let options = {
    url: 'http://146.56.230.35:3007/provider/upateProviderById',
    type: 'PUT',
    data: {
      id: parseInt(id),
      proCode: $('#proCode').val().trim(),
      proName: $('#proName').val().trim(),
      proContact: $('#proContact').val().trim(),
      proPhone: $('#proPhone').val().trim(),
      proAddress: $('#proAddress').val().trim(),
      proFax: $('#proFax').val().trim(),
      proDesc: $('#proDesc').val().trim(),
    },
  }
  console.log(options)
  //修改成功，跳转到用户列表页面
  location = 'providerList.html'
}

/**
 * 供应商编码验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProCode() {
  let ProCode = $('#proCode')
  ProCode[0].className = ''
  let v = ProCode.val()
  let div = ProCode.parent()
  let span = ProCode.next()
  span.empty()
  //判断空
  if (!v) {
    div[0].className = 'error'
    span.html('用户编码不能为空')
    return false
  }
  //数字字母下划线6-12位
  let reg = /^[$\w]{6,12}$/
  if (!reg.test(v)) {
    div[0].className = 'error'
    span.html('用户编码由数字、字母、_、$组成6-12位')
    return false
  }
  div[0].className = 'ok'
  span.html('用户编码正确')
  return true
}

/**
 * 供应商名称验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProName() {
  let ProName = $('#proName')
  ProName[0].className = ''
  let v = ProName.val()
  let div = ProName.parent()
  let span = ProName.next()
  span.empty()
  //判断空
  if (!v) {
    div[0].className = 'error'
    span.html('用户名不能为空')
    return false
  }
  //全英文或全汉字2-12位
  let reg = /^([\u4e00-\u9fa5]{2,12})$|^([a-zA-Z]{2,12})$/
  if (!reg.test(v)) {
    div[0].className = 'error'
    span.html('用户名全英文或全汉字2-12位')
    return false
  }
  div[0].className = 'ok'
  span.html('用户名正确')
  return true
}

/**
 * 联系电话验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProPhone() {
  let ProPhone = $('#proPhone')
  let v = ProPhone.val().trim()
  let parent = ProPhone.parent()
  let span = ProPhone.next()
  span.empty()
  //判断非空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('电话不能空')
    return false
  }
  //规则
  let reg = /^1[3456789]\d{9}$/
  if (!reg.test(v)) {
    parent.removeClass('ok').addClass('error')
    span.html('电话号码非法')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('电话正确')
  return true
}


/**
 * 联系人验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProContact() {
  let ProContact = $('#proContact')
  ProContact[0].className = ''
  let v = ProContact.val()
  let div = ProContact.parent()
  let span = ProContact.next()
  span.empty()
  //判断空
  if (!v) {
    div[0].className = 'error'
    span.html('联系人不能为空')
    return false
  }
  //全英文或全汉字2-12位
  let reg = /^([\u4e00-\u9fa5]{2,12})$|^([a-zA-Z]{2,12})$/
  if (!reg.test(v)) {
    div[0].className = 'error'
    span.html('联系人为全英文或全汉字2-12位')
    return false
  }
  div[0].className = 'ok'
  span.html('联系人正确')
  return true
}

/**
 * 联系地址验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProAddress() {
  let ProAddress = $('#proAddress')
  let v = ProAddress.val().trim()
  let parent = ProAddress.parent()
  let span = ProAddress.next()
  span.empty()
  //判断非空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('地址不能空')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('地址正确')
  return true
}

/**
 * 传真验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProFax() {
  let ProFax = $('#proFax')
  let v = ProFax.val().trim()
  let parent = ProFax.parent()
  let span = ProFax.next()
  span.empty()
  //判断非空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('地址不能空')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('地址正确')
  return true
}

/**
 * 描述验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProDesc() {
  let ProDesc = $('#proDesc')
  let v = ProDesc.val().trim()
  let parent = ProDesc.parent()
  let span = ProDesc.next()
  span.empty()
  //判断非空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('地址不能空')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('地址正确')
  return true
}

/**
 * 验证联系地址
 * @date 2021-05-08
 * @returns {any}
 */
function checkAddress() {
  let address = $('#userAddress')
  let v = address.val().trim()
  let parent = address.parent()
  let span = address.next()
  span.empty()
  //判断非空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('地址不能空')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('地址正确')
  return true
}

/**
 * 验证供应商电话
 * @date 2021-05-08
 * @returns {any}
 */
function checkPhone() {
  let phone = $('#userphone')
  let v = phone.val().trim()
  let parent = phone.parent()
  let span = phone.next()
  span.empty()
  //判断非空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('电话不能空')
    return false
  }
  let reg = /^1[3456789]\d{9}$/
  if (!reg.test(v)) {
    parent.removeClass('ok').addClass('error')
    span.html('电话号码非法')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('电话正确')
  return true
}

/**
 * 验证用户名 全英文或全中文 2-16位
 * @date 2021-05-08
 * @returns {any}
 */
function checkUserName() {
  let userName = $('#userName')
  let v = userName.val()
  let parent = userName.parent()
  let span = userName.next()
  span.empty()
  //判断空
  if (!v) {
    parent.removeClass('ok').addClass('error')
    span.html('用户名不能为空')
    return false
  }
  //全英文或全中文 2-16位
  let reg = /^([\u4e00-\u9fa5]{2,16})$|^([a-zA-Z_$]{2,16})$/
  if (!reg.test(v)) {
    parent.removeClass('ok').addClass('error')
    span.html('用户名全英文或全中文2-16位')
    return false
  }
  parent.removeClass('error').addClass('ok')
  span.html('用户名正确')
  return true
}

async function getUserById() {
  let {
    id
  } = getParams() //{id:'40'} 对象解构
  if (!id) {
    return null
  }
  //准备参数
  let options = {
    url: 'http://146.56.230.35:3007/provider/getProviderById',
    type: 'GET',
    data: {
      id: parseInt(id)
    },
  }
  try {
    let res = await request(options)
    console.log(res);
    if (res.code !== 200) {
      alert(res.msg)
      return null
    }
    return res.provider
  } catch (error) {
    return null
  }
}

async function bindUserToForm() {
  let user = await getUserById()
  console.log(user);
  if (!user) {
    return
  }
  //把用户的信息填充到表单中
  $('#proName').val(user.proName)
  // 编码
  $('#proCode').val(user.proCode)
  $('#proContact').val(user.proContact)
  $('#proPhone').val(user.proPhone)
  $('#proAddress').val(user.proAddress)
  $('#proFax').val(user.proFax)
  $('#proDesc').val(user.proDesc)

}


//获得url中的查询参数
function getParams() {
  //?id=40&name=tom
  let search = location.search
  if (!search) {
    return null
  }
  search = search.substring(1)
  let index = search.indexOf('&')
  let object = {}
  if (index === -1) {
    let arr = search.split('=')
    object[arr[0]] = arr[1]
  } else {
    //id=40&name=tom--->arr=['id=40','name=tom']
    let arr = search.split('&')
    for (let i = 0; i < arr.length; i++) {
      //'id=40'
      let item = arr[i]
      //'id=40'--->['id','40']
      let array = item.split('=')
      object[array[0]] = array[1]
    }
  }
  return object
}
